/*
 Xern Entity Relocation Network
 Copyright (C) 2011 Ronie Salgado <roniesalg@gmail.com>

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/


#ifndef XERN_HASHES_HPP
#define XERN_HASHES_HPP

#include <boost/shared_ptr.hpp>
#include "DataStream.hpp"
#include "IDataFilter.hpp"

namespace Xern
{
	/**
	 * Standard Sha256 hash object.
	 */
	class Sha256
	{
	public:
		static const int HashSize = 32;
		static const int StringSize = HashSize*2+1;

		Sha256()
		{
			for(int i = 0; i < HashSize; i++)
				data[i] = 0;
		}

		Sha256(const Sha256 &o)
		{
			for(int i = 0; i < HashSize; i++)
				data[i] = o.data[i];
		}

		~Sha256()
		{
		}

		bool IsEmpty() const
		{
			for(int i = 0; i < HashSize; i++)
			{
				if(data[i] != 0)
					return false;
			}
			return true;
		}

		const Sha256 &operator=(const Sha256 &o)
		{
			for(int i = 0; i < HashSize; i++)
				data[i] = o.data[i];

			return *this;
		}

		void ToString(char *dest, size_t maxlen) const
		{
			const char* HexTable = "0123456789abcdef";
			assert(maxlen > HashSize*2);
			for(int i = 0; i < 32; i++)
			{
				dest[i*2] = HexTable[(data[i] & 0xF0)>>4];
				dest[i*2+1] = HexTable[data[i] & 0x0F];
			}

			// Write the null terminator.
			dest[64] = 0;
		}

		bool operator==(const Sha256 &o)
		{
			for(int i = 0; i < HashSize; i++)
				if(data[i] != o.data[i])
					return false;
			return true;
		}

		bool operator!=(const Sha256 &o)
		{
			for(int i = 0; i < HashSize; i++)
				if(data[i] != o.data[i])
					return true;
			return false;
		}

		bool operator<(const Sha256 &o)
		{
			for(int i = 0; i < HashSize; i++)
			{
				if(data[i] < o.data[i])
					return true;
				if(data[i] > o.data[i])
					return false;
			}
			return false;
		}

		static Sha256 Parse(const char *src, size_t len)
		{
			Sha256 ret;
			for(int i = 0; i < len/2; i++)
			{
				int byte;
				sscanf(src + i*2, "%2x", &byte);
				ret.data[i] = byte;
			}
			return ret;
		}

		static Sha256 Parse(const std::string &hash)
		{
			return Parse(hash.c_str(), hash.size());
		}

		static Sha256 Digest(boost::shared_ptr<IDataFilter> file);

		u8 data[HashSize];
	};

	/**
	 * Standard MD5 hash object.
	 */
	class Md5
	{
	public:
		static const int HashSize = 16;

		Md5()
		{
			for(int i = 0; i < HashSize; i++)
				data[i] = 0;
		}

		Md5(const Md5 &o)
		{
			for(int i = 0; i < HashSize; i++)
				data[i] = o.data[i];
		}

		~Md5()
		{
		}

		bool IsEmpty() const
		{
			for(int i = 0; i < HashSize; i++)
			{
				if(data[i] != 0)
					return false;
			}
			return true;
		}

		const Md5 &operator=(const Sha256 &o)
		{
			for(int i = 0; i < HashSize; i++)
				data[i] = o.data[i];

			return *this;
		}

		static Md5 Digest(boost::shared_ptr<IDataFilter> file);

		u8 data[HashSize];
	};

	// Hash object I/O.
	namespace detail
	{
		template <bool BIG>
		struct ValueReader<Sha256, BIG>
		{
			template<typename STREAM>
			static bool Read(STREAM &in, Sha256 &value)
			{
				return in.Read(Sha256::HashSize, value.data);
			}
		};

		template <bool BIG>
		struct ValueReader<Md5, BIG>
		{
			template<typename STREAM>
			static bool Read(STREAM &in, Md5 &value)
			{
				return in.Read(Md5::HashSize, value.data);
			}
		};

		template <bool BIG>
		struct ValueWriter<Sha256, BIG>
		{
			template<typename STREAM>
			static bool Write(STREAM &out, const Sha256 &value)
			{
				return out.Write(Sha256::HashSize, value.data);
			}
		};

		template <bool BIG>
		struct ValueWriter<Md5, BIG>
		{
			template<typename STREAM>
			static bool Write(STREAM &out, const Md5 &value)
			{
				return out.Write(Md5::HashSize, value.data);
			}
		};
	};
};

#endif //XERN_HASHES_HPP
